<template>
	<view>
		<page-header :headInfo="headInfo"></page-header>
		<model-calendar ref="qiandao" :sendYear="toYear" :sendMonth="toMonth" :dataSource="signData" :totalNum="sumCount" @dateChange="getData"
		 @clickChange="clickRegister">
		</model-calendar>
		<view class="rules">
			<view>未签到</view>
			<view>已签到</view>
			<view>待签到</view>
		</view>
		<view class="signInBtn" v-if="!isSignIn" @tap="signIn">签到</view>
		<view class="signInBtn" v-if="isSignIn">已签到</view>
	</view>
</template>

<script>
	// 引入日历插件
	import modelCalendar from '@/components/Calendar.vue';
	export default {
		components: {
			modelCalendar
		},
		data() {
			return {
				headInfo: {
					type: 0,
					title: 'iLove'
				},
				toYear: parseInt(new Date().getFullYear()), //本日
				toMonth: parseInt(new Date().getMonth() + 1), //本月
				sumCount: 0,
				signData: [],
				isSignIn:false
			};
		},
		onLoad() {
			//获取当前用户当前任务的签到状态  			
			this.getData(this.toYear + "-" + this.toMonth);
		},
		methods: {
			clickRegister(day) {
				this.isSignIn = true;
				this.signData.push(day);
				this.sumCount++
			},
			
			/**
			 * @author smallfour
			 * @description 签到
			 */
			signIn:function(){
				let date = this.getDate();
				this.$refs.qiandao.clickSignUp(date,1);
			},
			
			/**
			 * @author smallfour
			 * @description 获取时间
			 */
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
			
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},


			/**
			 * @author smallfour
			 * @description 获取当前用户该任务的签到数组
			 */
			getData(val) {
				let y = val.split('-')[0];
				let m = val.split('-')[1];
				this.sumCount = 88;
				let obj = {
					years: y,
					months: m
				}
				m < 10 ? (m = '0' + m) : m;
				this.$api.signList(obj).then(res => {
					let num = [];
					for (let k in res.data) {
						if (res.data[k].days < 10) {
							num.push('0' + res.data[k].days);
						} else {
							num.push(String(res.data[k].days));
						}
					}
					let newSign = [],
						today = new Date().getDate();
					for (let i = 0; i < num.length; i++) {
						// 判断今天是否签到
						if((y + "-" + m + "-" + num[i]) == this.getDate()){
							this.isSignIn = true;
						}
						newSign.push(y + "-" + m + "-" + num[i])
					}
					this.signData = newSign;
				})
			}
		}
	};
</script>

<style lang="scss">
	.count .daynumber {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.count .daynumber .day {
		margin-top: 50rpx;
	}

	.count {
		margin: 20rpx;
		padding: 30rpx;
		display: flex;
		text-align: center;
		border-radius: 10px;
		flex-direction: column;
		justify-content: center;
		background-color: #fff;
		align-items: center;
	}

	.count .number {
		color: #fff;
		font-size: 60rpx;
		background-color: #94db98;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		margin: 20rpx;
	}

	.monthSum {
		color: red;
		font-size: 40rpx;
	}

	.count text {
		margin: 10rpx;
	}

	.rules {
		padding:10rpx 4.5vw;
		display: flex;
		align-items: center;
		font-size: 28rpx;
		line-height: 60rpx;
	}
	.rules>view {
		margin-right: 40rpx;
		flex: 1;
		text-align: center;
	}
	.rules>view::before {
		display: inline-block;
		content: "";
		width: 16rpx;
		height: 16rpx;
		border-radius: 50%;
		margin-right: 6rpx;
	}
	.rules>view:nth-child(1)::before{
		background-color: #607699;
	}
	.rules>view:nth-child(2)::before{
		background-color: #fc6c53;
	}
	.rules>view:nth-child(3)::before{
		background-color: #b4b4b4;
	}
	
	.signInBtn{
		margin: 20rpx 4.5vw;
		height: 10vw;
		line-height: 10vw;
		border-radius: 10vw;
		text-align: center;
		background-color: #ec6e81;
		color: #FFFFFF;
	}
</style>
